angular.module('Controller')
.controller('QgyPriceManageModalCtrl',($scope,urls,$localStorage,NgTableParams,commonUtil,cryptUtil,wfAlert,commonConstant,$http, $uibModalInstance, items)->
    $scope.type=type=items.type
    $scope.data=data=items.data
    if data.ruleType==1
        $scope.modalTitle='定价策略'
        $scope.modalSettleWayList=commonConstant.priceSettleWayList()
    else if data.ruleType==2
        $scope.modalTitle='优惠活动'
        $scope.modalSettleWayList=commonConstant.promotionSettleWayList()
    else if data.ruleType==3
        $scope.modalTitle='返利规则'
        $scope.modalSettleWayList=commonConstant.rebateSettleWayList()
    else
        data.ruleType=1
        $scope.modalTitle='定价策略'
        $scope.modalSettleWayList=commonConstant.priceSettleWayList()
    if type=='view'
        $scope.modalTitle=$scope.modalTitle+'详情'
    else if type=='new'
        $scope.modalTitle='新建'+$scope.modalTitle
    else
        $scope.modalTitle='编辑'+$scope.modalTitle

    data.startTime=new Date(data.startTime) if data.startTime
    data.endTime=new Date(data.endTime) if data.endTime

    $scope.modalLeaseBinList=commonConstant.leaseBinList()

    $scope.modalChannelList=commonConstant.priceChannelList()
    $scope.modalPayWayList=commonConstant.payWayList()
    $scope.modalStatusList=commonConstant.priceStatusList()
    cityList=commonConstant.qgyCityList()
    $scope.cityList=[value:0,name:'全部'].concat(cityList)
    allApartmentList=[]
    loadApartment=()->
        $scope.modalApartmentList=[{id:0,name:'全部'}]
        $http.get('/apartment',params:{page:0,size:999,enabled:true})
        .success (resp)->
            if resp.code==0
                $scope.modalApartmentList=$scope.modalApartmentList.concat(resp.message.content)
                allApartmentList=angular.copy($scope.modalApartmentList)
                console.log allApartmentList
                $scope.loadCityApart()
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadApartment()
    $scope.loadCityApart=()->
        console.log data,allApartmentList,cityList
        if data.city
            $scope.modalApartmentList=_.filter(allApartmentList,(theAp)->(theAp.city+'').substr(0,2)==(data.city+'').substr(0,2)||theAp.id==0)
            data.storeId=0 if data.type=='new'
        else
            $scope.modalApartmentList=angular.copy(allApartmentList)
    initNewData=()->
        return if type!='new'
        data.city=0 if data.city==undefined
        data.storeId=0 if data.storeId==undefined
        data.settleWay=$scope.modalSettleWayList[0].value if data.settleWay==undefined
        data.status=$scope.modalStatusList[0].value if data.status==undefined
    initNewData()
    $scope.newData=()->
        if data.channel==0 || data.channel==undefined
            wfAlert.toastError('至少使用一个渠道')
            return
        if data.lease==0 || data.lease==undefined
            wfAlert.toastError('至少使用一个租期')
            return
        if data.payWay==0 || data.payWay==undefined
            wfAlert.toastError('至少使用一个付款方式')
            return
        reqData={
            "ruleType": data.ruleType
            "ruleName": data.ruleName
            "description": data.description
            "startTime": if data.startTime then moment(data.startTime).format('YYYY-MM-DD') else undefined
            "endTime": if data.endTime then moment(data.endTime).format('YYYY-MM-DD') else undefined
            "city":data.city
            "storeId": data.storeId
            "payWay":data.payWay
            "lease": data.lease
            "settleWay": data.settleWay
            "money": data.money
            "minMoney":data.minMoney
            "channel": data.channel
            "status": data.status
        }
        $http.post("/housePrice",reqData)
        .success((resp)->
            if resp.code==0
                wfAlert.toastInfo($scope.modalTitle+'成功')
                $uibModalInstance.close('done')
            else
                wfAlert.toastError(resp.message)
        )
    $scope.editData=()->
        reqData={
            "status": data.status
        }
        $http.put("/housePrice/#{data.id}",reqData)
        .success((resp)->
            if resp.code==0
                wfAlert.toastInfo($scope.modalTitle+'成功')
                $uibModalInstance.close('done')
            else
                wfAlert.toastError(resp.message)
        )
    $scope.confirm=()->
        if type == 'new'
            $scope.newData()
        else if type =='edit'
            $scope.editData()
        else
            $uibModalInstance.dismiss('cancel')
    $scope.cancel =()->
        $uibModalInstance.dismiss('cancel')
)